package com.jilian.mydemo.modul.main.ui;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseEventMsg;
import com.jilian.mydemo.base.BaseFragment;
import com.jilian.mydemo.base.CommonViewPagerAdapter;
import com.jilian.mydemo.event.RxBus;
import com.jilian.mydemo.modul.order.ui.FiveOrderFragment;
import com.jilian.mydemo.modul.order.ui.FourOrderFragment;
import com.jilian.mydemo.modul.order.ui.MapViewActivity;
import com.jilian.mydemo.modul.order.ui.OneOrderFragment;
import com.jilian.mydemo.modul.order.ui.ThreeOrderFragment;
import com.jilian.mydemo.modul.order.ui.TwoOrderFragment;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.DisplayUtil;
import com.jilian.mydemo.views.NoScrollViewPager;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;


public class TwoFragment extends BaseFragment {

    private NoScrollViewPager mViewPager;
    private List<Fragment> fragmentList;
    private OneOrderFragment oneOrderFragment;
    private TwoOrderFragment twoOrderFragment;
    private ThreeOrderFragment threeOrderFragment;
    private FourOrderFragment fourOrderFragment;
    private FiveOrderFragment fiveOrderFragment;
    String[] mTitleDataList = new String[5];
    private MagicIndicator magicIndicator;
    private CommonViewPagerAdapter adapter;
    private CommonNavigator commonNavigator;
    private OrderViewModel orderViewModel;

    @Override
    protected void loadData() {

    }

    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_two;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        setCenterTitle(view, "订单", "#FFFFFF");
        setleftTitle(view, "工单地图", "#FFFFFF", false, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MapViewActivity.class));
            }
        });
        mViewPager = (NoScrollViewPager) view.findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(5);
        magicIndicator = (MagicIndicator) view.findViewById(R.id.magic_indicator);

        initViewPager();
        initTabData();
        initIndicator();

        //关联
        magicIndicator.setNavigator(commonNavigator);

        ViewPagerHelper.bind(magicIndicator, mViewPager);

    }

    private void initIndicator() {
        commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(getResources().getColor(R.color.color_hint));
                colorTransitionPagerTitleView.setSelectedColor(getResources().getColor(R.color.color_main_title));
                colorTransitionPagerTitleView.setText(mTitleDataList[index]);
                colorTransitionPagerTitleView.setTextSize(14);
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                //设置indicator的宽度
                indicator.setLineWidth(DisplayUtil.dip2px(context, 40));
                indicator.setColors(getResources().getColor(R.color.color_main_title));
                return indicator;
            }
        });


    }

    /**
     * 初始化ViewPager 各个页面
     */
    private void initViewPager() {
        fragmentList = new ArrayList<>();
        oneOrderFragment = new OneOrderFragment();
        twoOrderFragment = new TwoOrderFragment();
        threeOrderFragment = new ThreeOrderFragment();
        fourOrderFragment = new FourOrderFragment();
        fiveOrderFragment = new FiveOrderFragment();
        fragmentList.add(oneOrderFragment);
        fragmentList.add(twoOrderFragment);
        fragmentList.add(threeOrderFragment);
        fragmentList.add(fourOrderFragment);
        fragmentList.add(fiveOrderFragment);
        adapter = new CommonViewPagerAdapter(getChildFragmentManager(), fragmentList);
        mViewPager.setAdapter(adapter);
    }

    /**
     * 初始化tab的数据
     */
    private void initTabData() {
        mTitleDataList[0] = "待抢单";
        mTitleDataList[1] = "待预约";
        mTitleDataList[2] = "进行中";
        mTitleDataList[3] = "已完成";
        mTitleDataList[4] = "已取消";
    }

    @Override
    protected void initData() {
        RxBus.getInstance().toObservable().map(new Function<Object, BaseEventMsg>() {
            @Override
            public BaseEventMsg apply(Object o) throws Exception {
                return (BaseEventMsg) o;
            }
        }).subscribe(new Consumer<BaseEventMsg>() {
            @Override
            public void accept(BaseEventMsg eventMsg) throws Exception {
                if (eventMsg != null&&eventMsg.getMonthOrderMsgDto()!=null) {
                    if (eventMsg.getMonthOrderMsgDto().getCode() == 100) {
                        mViewPager.setCurrentItem(3);
                    }
                }
            }
        });
    }


    /**
     * 获取订单状态
     *
     * @return
     */
    private Integer getStatus() {
        Integer status = null;
        //2.待抢单， 3 待预约，4 进行中 5 已完成 7 已取消
        //响应数据
        switch (mViewPager.getCurrentItem()) {
            case 0:
                status = 2;
                break;
            case 1:
                status = 3;
                break;
            case 2:
                status = 4;
                break;
            case 3:
                status = 5;
                break;
            case 4:
                status = 7;
                break;
        }
        return status;
    }

    @Override
    protected void initListener() {


    }
}
